package com.example.hookapp;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;

import com.example.hookapp.base.BaseFloatMainView;
import com.example.hookapp.base.HookLogMessageFragment;


public class FloatMainView extends BaseFloatMainView {
    private String action;

    public FloatMainView(Context context, String action) {
        super(context);
        if(!isHide){
            switchFragment(0, HookLogMessageFragment.class);
        }
        this.action = action;
    }

    @Override
    protected void initTabs() {
        addTab("日志(点击显示)", HookLogMessageFragment.class);
        addTab("清空日志");
        addTab("拦截点击");
    }

    @Override
    protected void fragmentShow(int index, Class fragmentClass) {
        super.fragmentShow(index, fragmentClass);
        if (HookLogMessageFragment.class == fragmentClass) {
            setCanMove(false);
            tabItemList.get(index).getBindTextView().setText("日志(点击收起)");
            tabItemList.get(index).getBindTextView().setTextColor(0xffFFD700);
            isHide = false;
        }
    }

    @Override
    protected void fragmentHide(int index, Class fragmentClass) {
        super.fragmentHide(index, fragmentClass);
        if (HookLogMessageFragment.class == fragmentClass) {
            setCanMove(true);
            tabItemList.get(index).getBindTextView().setText("日志(点击显示)");
            tabItemList.get(index).getBindTextView().setTextColor(0xffffffff);
            isHide = true;
        }
    }

    static boolean isInterceptClick = false;
    static boolean isHide;

    @Override
    protected boolean interceptTab(TabItem tabItem) {
        switch (tabItem.getTabName()) {
            case "拦截点击":
                if (!isInterceptClick) {
                    tabItem.getBindTextView().setText("不拦截点击");
                    tabItem.getBindTextView().setTextColor(0xffFFD700);
                    context.sendBroadcast(new Intent("InterceptClick"));
                } else {
                    tabItem.getBindTextView().setText("拦截点击");
                    tabItem.getBindTextView().setTextColor(0xffffffff);
                    context.sendBroadcast(new Intent("CancelInterceptClick"));
                }
                postDelayed(() -> {
                    isInterceptClick = !isInterceptClick;
                }, 1000);
                return true;
//            case "清空日志":
//                EditTextCallbackDialog.newInstance((Activity) context, (text, dialog) -> {
//                    setVisibility(View.GONE);
//                    dialog.dismiss();
//                }).hint("输入主播房间id").title("消失吧").show();
//
//                return true;
            case "清空日志":
//                Intent intent = new Intent(action);
//                intent.putExtra("type", tabItem.getTabName());
//                context.sendBroadcast(intent);
//                setVisibility(View.GONE);
                Fragment fragment = findFragment(HookLogMessageFragment.class);
                if (fragment instanceof HookLogMessageFragment) {
                    ((HookLogMessageFragment) fragment).clearLog();
                }
                return true;

        }
        return false;
    }
}
